﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using BTN04.BUS;
using BTN04.DTO;


namespace BTN04.ADMIN
{
    public partial class quanlygianhang : System.Web.UI.Page
    {
        int index;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Hien thi danh sach tinh trang loai san pham len droplist
            List<TinhTrangLoaiSanPhamDTO> ds = new List<TinhTrangLoaiSanPhamDTO>();
            TinhTrangLoaiSanPhamBUS trlspBus = new TinhTrangLoaiSanPhamBUS();
            ds = trlspBus.DanhSachTinhTrangLoaiSanPham();
            if (!IsPostBack)
            {
                for (int i = 0; i < ds.Count; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = ds[i].MaTinhTrangLoaiSanPham.ToString();
                    lv.Text = ds[i].TenTinhTrangLoaiSanPham.ToString();
                    drTinhTrangLSP.Items.Add(lv);
                }
            }

        }

        protected void ChonLoaiSanPham(object sender, EventArgs e)
        {
            
            LoaiSanPhamBUS lspBus = new LoaiSanPhamBUS();
            index = GridView1.SelectedIndex;
            GridViewRow row = GridView1.SelectedRow;

            if (index >= 0)
            {
                int ma = int.Parse(row.Cells[0].Text);
                LoaiSanPhamDTO lspDto = new LoaiSanPhamDTO();
                lspDto = lspBus.HienThiChiTietLoaiSanPhamTheoMa(ma);
                txtMaLoaiSP.Text = lspDto.MaLoaiSanPham.ToString();
                txtTenLoaiSP.Text = lspDto.TenLoaiSanPham.ToString();
                drTinhTrangLSP.Items[drTinhTrangLSP.SelectedIndex].Text = lspDto.TenTinhTrangLoaiSanPham.ToString();

            }


        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            if(txtMaLoaiSP.Text!="")
            {
                LoaiSanPhamBUS lspBus = new LoaiSanPhamBUS();
                int ma = int.Parse(txtMaLoaiSP.Text);
                lspBus.XoaMotLoaiSanPham(ma);
                Response.Redirect("~/ADMIN/quanlygianhang.aspx");

            }
            else
            {
                ThongBao2.Text = "Chưa chọn loại sản phẩm";
            }            


        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            if(txtMaLoaiSP.Text!="")
            {
                LoaiSanPhamBUS lspBus = new LoaiSanPhamBUS();
                LoaiSanPhamDTO lspDto = new LoaiSanPhamDTO();
                lspDto.MaLoaiSanPham = int.Parse(txtMaLoaiSP.Text);
                lspDto.TenLoaiSanPham = txtTenLoaiSP.Text;
                lspDto.MaLoaiTinhTrangSanPham = int.Parse(drTinhTrangLSP.Items[drTinhTrangLSP.SelectedIndex].Value.ToString());
                lspBus.CapNhatThongTinMotLoaiSanPham(lspDto);
                Response.Redirect("~/ADMIN/quanlygianhang.aspx");

            }
            else
            {
                ThongBao2.Text = "Chưa chọn loại sản phẩm";
            }
        

        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            LoaiSanPhamDTO lspDto = new LoaiSanPhamDTO();
            LoaiSanPhamBUS lspBus = new LoaiSanPhamBUS();
            lspDto.MaLoaiTinhTrangSanPham = 1;
            lspDto.TenLoaiSanPham = txtTenLoaiSanPhamMoi.Text;
            if (txtTenLoaiSanPhamMoi.Text != "")
            {

                int kq = lspBus.ThemMotLoaiSanPham(lspDto);
                Response.Redirect("~/ADMIN/quanlygianhang.aspx");
            }
            else
            {
                ThongBao1.Text = "Tên Loại Sản Phẩm Không để rỗng";
            }
           
        }
    }
}
